﻿using System.IO;
using Framework.Core.Cryptography;
using Xunit;

namespace Framework.Tests.Core.Cryptography
{
    public class AssymetricEncryptionTest
    {
        [Fact]
        public void Should_encrypt_decrypt_string_with_internal_public_key()
        {
            using (var stream = new MemoryStream())
            {
                using (var reader = new AssymetricDecryptionReader(stream, "dealcapture", "params_m"))
                {
                    using (var writer = new AssymetricEncryptionWriter(stream, "dealcapture", "params_m"))
                    {
                        const string expected = "hello";
                        writer.Write(expected);

                        Assert.Equal(expected, reader.ReadToEnd());
                    }
                }
            }
        }

        [Fact]
        public void Should_encrypt_decrypt_string_with_supplied_public_key()
        {
            using (var publicKeyBuilder = new AssymetricPublicKeyBuilder("dealcapture", "params_m"))
            {
                using (var publicKeyStream = new MemoryStream())
                {
                    using (var stream = new MemoryStream())
                    {
                        using (var reader = new AssymetricDecryptionReader(stream, "dealcapture", "params_m"))
                        {
                            using (
                                var writer = new AssymetricEncryptionWriter(stream,
                                                                            publicKeyBuilder.ExportPublicKey(
                                                                                publicKeyStream)))
                            {
                                const string expected = "hello";
                                writer.Write(expected);

                                Assert.Equal(expected, reader.ReadToEnd());
                            }
                        }
                    }
                }
            }
        }
    }
}
